<?php
/**
 * 门店
 * 苑立彬
 * 2022/7/29
 */

namespace App\Repository\Repositories;

use App\Http\Serve\Es;
use App\Models\StoreProduct;
use App\Models\SystemStore;
use App\Repository\RepositoryInterface\SystemStoreInterface;

class SystemStoreRepositories implements SystemStoreInterface
{
    protected $systemStore;

    public function __construct(SystemStore $systemStore)
    {
        $this->systemStore = $systemStore;
    }


    /**
     * es 搜索
     * @param $params
     * @return array|void
     */
    public function search($params){
        if (!empty($params)){
            $body = [
                'query' => [
                    'match' => [
                        'name' => [
                            'query' => $params
                        ]
                    ]
                ],
                'highlight'=>[
                    'fields'=>[
                        'name'=>[
                            'pre_tags'=>[
                                '<span style="color: red">'
                            ],
                            'post_tags'=>[
                                '</span>'
                            ]
                        ]
                    ]
                ]
            ];
            //调用es的搜索
            $res = (new Es())->search_doc('head_doc','head_list',$body);
            $data = array_column($res['hits']['hits'], '_source');
            foreach ($data as $key=>&$v){
                $v['name'] = $res['hits']['hits'][$key]['highlight']['name'][0];
            }
            unset($v);
            return $data;
        }
    }
    /**
     * 门店列表
     * @param $num
     * @param $name
     * @return mixed
     * @throws \Exception
     */
    public function systemStoreList($num, $name)
    {
        $where = [];
        if (!empty($name)) {
            $where[] = ['name', 'like', "%$name%"];
        }
        $data = SystemStore::list($num, $where);
        if (!$data) {
            throw  new  \Exception('查询失败');
        }
        return $data;
    }

    /**
     * 门店添加
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function systemStoreCreate($data)
    {
        $data = SystemStore::create_store($data);
        if (!$data) {
            throw new \Exception('添加失败');
        }
        $es = new Es();
        if (!$es->exists_index('head_list')) {
            //创建es索引，es的索引相当于MySQL的数据库
            $es->create_index('head_list');
        }
        $es->add_doc($data->id, json_decode($data), 'head_doc', 'head_list');
        return $data;
    }

    /**
     * 门店商品删除
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function systemStoreDel($id)
    {
        $data = StoreProduct::systemDelModel($id);
        if (!$data) {
            throw  new  \Exception('删除失败');
        }
        return $data;
    }

    /**
     * 修改数据展示
     * @param $id
     * @param $is_show
     * @return mixed
     * @throws \Exception
     */
    public function systemIsShow($id)
    {
        $result = SystemStore::editModel($id);
        if (!$result) {
            throw  new  \Exception('查询错误');
        }
        return $result;
    }

    /**
     * 门店审核
     * @param $id
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public function update_status($id, $data)
    {
        $res = SystemStore::update_status($id, $data);
        if (!$res) {
            throw  new \Exception('审核错误');
        }
        return $res;
    }

    /**
     * 门店删除
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function systemDel($id)
    {
        $res = SystemStore::delModel($id);
        if (!$res) {
            throw  new \Exception('删除失败');
        }
        return $res;
    }

    /**
     * 门店商品上下架
     * @param $id
     * @param $is_show
     * @return mixed
     */
    public function systemProductUpdate($id, $is_show)
    {
        if (empty($id)) {
            throw  new \Exception('参数错误');
        }
        if (empty($is_show)) {
            throw  new \Exception('参数错误');
        }
        $result = StoreProduct::systemIsShow($id, $is_show);
        if (!$result) {
            throw  new  \Exception('错误');
        }
        return $result;
    }

    /**
     * @param $id
     * @param $data
     * @return mixed|void
     */
    public function storeUpdate($id, $data)
    {
        $result = SystemStore::storeUpdate($id, $data);
        if (!$result) {
            throw  new  \Exception('修改失败');
        }
        return $result;
    }
}
